<template>
  <div class="department-container">
    <div class="app-container">
      <el-card shadow="always" :body-style="{ padding: '20px' }">
        <div slot="header">
          <el-row type="flex" justify="space-between" align="middle">
            <span>权限管理</span>
            <el-button type="primary" @click="addPermission(1,'0')">添加权限</el-button>
          </el-row>
        </div>
        <!-- card body -->
        <!-- rowkey属性开启渲染树形表格 值为一个唯一值(id) -->
        <el-table border :data="treeList" row-key="id">
          <el-table-column prop="name" label="名称" />
          <el-table-column prop="code" label="标识" />
          <el-table-column prop="description" label="描述" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button type="text" @click="addPermission(2,row.id)">添加</el-button>
              <el-button type="text" @click="editPermission(row)">编辑</el-button>
              <el-button type="text" @click="delPermission(row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <el-dialog
        :title="form.id?'编辑权限':'新增权限'"
        :visible.sync="isShowDialog"
        width="30%"
        @close="closeDialog"
      >
        <el-form ref="form" :model="form" :rules="rules" label-width="80px" :inline="false" size="normal">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item label="权限表示">
            <el-input v-model="form.code"></el-input>
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="form.description"></el-input>
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              active-text="启用"
              active-value="1"
              inactive-text="不启用"
              inactive-value="0"
            >
            </el-switch>

          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">确定</el-button>
            <el-button @click="closeDialog">取消</el-button>
          </el-form-item>
        </el-form>

      </el-dialog>

    </div>
  </div>
</template>
<script>
import { getPermissionListAPI, addPermissionAPI, delPermissionAPI, getPermissionDetailAPI, updatePermissionAPI } from '@/api/permisson'
import { tranListToTreeData } from '@/utils/index'
export default {
  data() {
    return {
      isShowDialog: false, // 控制弹出层
      treeList: [], // 树形结构数据
      form: { // 新增权限要提交的数据
        enVisible: '0', // 是否启用
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 判断是父权限还是子权限
        pid: '' // 添加到哪个节点下

      },
      rules: {
        name: [{ required: true, min: 2, max: 10, message: '请输入名称', trigger: ['change', 'blur'] }]
      }
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    // 调用获取数据的API
    async getPermissionList() {
      const data = await getPermissionListAPI()
      console.table(data)
      this.treeList = tranListToTreeData(data)
    },
    // 关闭弹出层
    closeDialog() {
      this.isShowDialog = false
      console.log(1)
      // 重置数据
      this.$refs.form.resetFields()
      this.form = {
        enVisible: '0', // 是否启用
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 判断是父权限还是子权限
        pid: '' // 添加到哪个节点下
      }
    },
    // 添加权限
    // 根据传入的type和id来区分是添加主权限还是子权限
    addPermission(type, id) {
      this.isShowDialog = true
      console.log(type, id)
      this.form.type = type
      this.form.pid = id
    },
    // 编辑权限
    async editPermission(row) {
      // 先根据id获取相对应的数据
      const data = await getPermissionDetailAPI(row.id)
      // 数据回填
      this.form = data
      console.log(this.form)
      // 执行顺序问题
      setTimeout(() => {
        this.isShowDialog = true
      }, 50)
    },
    // 删除权限
    async delPermission(row) {
      this.$confirm('确认要删除吗?', '提示').then(async() => {
        if (row.children && row.children.length) {
          this.$message.error('不能直接删除父节点')
        } else {
          await delPermissionAPI(row.id)
          this.$message.success('删除成功')
          this.getPermissionList()
        }
      }).catch(console.log)
    },
    // 点击确认后调用添加权限的API
    onSubmit () {
      this.$refs.form.validate(async(isOK) => {
        if (isOK) {
          // 通过传入的数据是否有id来判断是编辑还是新增
          if (this.form.id) { // 如果数据中有id这个属性,说明是调用编辑接口获得的数据
            // 所以需要调用更新数据接口
            await updatePermissionAPI(this.form)
            this.$message.success('权限修改成功')
          } else {
            const res = await addPermissionAPI(this.form)
            console.log(res)
            this.$message.success('权限添加成功')
          }

          this.getPermissionList()
          this.isShowDialog = false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
